package com.dl7.playerdemo;

import com.bumptech.glide.Glide;
import com.dl7.player.media.IjkPlayerView;
import com.dl7.playerdemo.adapter.ListAdapter;
import com.dl7.playerdemo.utils.ScreenUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.StackLayout;
import ohos.agp.window.service.WindowManager;
import ohos.global.configuration.Configuration;
import ohos.multimodalinput.event.KeyEvent;

import java.util.ArrayList;
import java.util.List;

public class TestAspectActivity extends Ability {

    private static final String VIDEO_URL = "http://flv2.bn.netease.com/videolib3/1505/29/DCNOo7461/SD/DCNOo7461-mobile.mp4";
    private static final String IMAGE_URL = "http://vimg3.ws.126.net/image/snapshot/2015/5/J/M/VAPRJCSJM.jpg";

    private IjkPlayerView mPlayerView;
    private ListContainer mListView;
    private ListAdapter mAdapter;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_activity_test_aspect);
        WindowManager.getInstance().getTopWindow().get().setTransparent(true);
        mPlayerView = (IjkPlayerView) findComponentById(ResourceTable.Id_test_aspect_player_view);
        mListView = (ListContainer) findComponentById(ResourceTable.Id_lv_list);

        List<String> list = new ArrayList<>();
        for (int i = 0; i < 40; i++) {
            list.add("Some message " + i);
        }
        mAdapter = new ListAdapter(this, list);
        mListView.setItemProvider(mAdapter);

        Glide.with(this).load(IMAGE_URL).fitCenter().into(mPlayerView.mPlayerThumb);
        mPlayerView.init()
                .setTitle("美加州死亡谷石头会走路")
                .setVideoPath(VIDEO_URL);
        mPlayerView.initDamukuView("resources/rawfile/bili.xml");
    }

    @Override
    protected void onActive() {
        super.onActive();
        mPlayerView.onResume();
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        mPlayerView.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPlayerView.onDestroy();
    }

    @Override
    public void onConfigurationUpdated(Configuration configuration) {
        int screenWidth = ScreenUtils.getScreenWidth(this);
        int screenHeight = ScreenUtils.getScreenHeight(this);

        super.onConfigurationUpdated(configuration);

        if (configuration.direction == Configuration.DIRECTION_HORIZONTAL) {
            mPlayerView.setLayoutConfig(new StackLayout.LayoutConfig(screenWidth, screenHeight));
        } else {
            mPlayerView.setLayoutConfig(new StackLayout.LayoutConfig(screenWidth, 600));
        }

        mPlayerView.configurationChanged(configuration);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mPlayerView.handleVolumeKey(keyCode)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        if (mPlayerView.onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }
}
